package com.woniu.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.errorprone.annotations.Var;
import com.woniu.bean.R;
import com.woniu.order.bean.*;
import com.woniu.order.service.ITGoodsService;
import com.woniu.order.service.ITLogisticsService;
import com.woniu.order.service.ITOrderService;
import com.woniu.order.service.ITOrderbasicinfoService;
import com.woniu.order.util.RedisSeachUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellowDocter
 * @since 2022-08-09
 */
@CrossOrigin
@RestController
@RequestMapping("/tLogistics")
public class TLogisticsController {
    @Autowired
    private ITLogisticsService logisticsService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private ITGoodsService itGoodsService;
    @Autowired
    private ITOrderService itOrderService;
    @Autowired
    private ITOrderbasicinfoService itOrderbasicinfoService;
    @Autowired
    private RedisSeachUtil redisSeachUtil;

    @GetMapping
    public R add() throws JsonProcessingException {
        System.out.println("TOrderbasicinfoController.add");
        List<TOrder> ary = itOrderService.list();
        Integer orderId = ary.get(ary.size() - 1).getOrderId();
        orderId+=1;
        System.out.println("orderId = " + orderId);
        String logisticsKey = "logistics:"+ orderId + ":save";
        R r=null;
        System.out.println(StringUtils.hasText(stringRedisTemplate.opsForValue().get(logisticsKey)));
        if(!StringUtils.hasText(stringRedisTemplate.opsForValue().get(logisticsKey))){
            System.out.println("aaa");
            OverAll redis = redisSeachUtil.getRedis(orderId);
            System.out.println("redis = " + redis);
            TLogistics tLogistics=new TLogistics();
            String s = redis.getAddress().gettAddrinfo().getCity()+"-"+redis.getAddress().getfAddrinfo().getCity();
            tLogistics.setRoute(s);
            tLogistics.setTransType(redis.gettGoods().getTranType());
            tLogistics.setCreatiomTime(redis.gettOrderbasicinfo().getReceiveTime());
            tLogistics.setOrderId(orderId);
            String s2 = objectMapper.writeValueAsString(tLogistics);
            stringRedisTemplate.opsForValue().set(logisticsKey,s2);
            List<TLogistics> list=new ArrayList<>();
            list.add(tLogistics);
            Map map=new HashMap<>();
            map.put("log", list);
            r=R.OK();
            return r;
        }
            if(StringUtils.hasText(stringRedisTemplate.opsForValue().get(logisticsKey))){
                String logis = stringRedisTemplate.opsForValue().get(logisticsKey);
                System.out.println("logis = " + logis);
                TLogistics tLogistics = objectMapper.readValue(logis, TLogistics.class);
                List<TLogistics> list=new ArrayList<>();
                list.add(tLogistics);
                System.out.println("list = " + list);
                Map map=new HashMap<>();
                map.put("log", list);
                r=R.OK();
                r.setData(map);
            } else {
                r=R.ERROR();
            }
            return r;
        }





}
